package com.yanqu.road.server.manager.cricket.entity.dao;

import com.fasterxml.jackson.core.type.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.dbV2.serializer.Helper;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.logic.model.cricket.CricketUserBaseInfo;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.manager.cricket.entity.DbCricketHotBattleLog;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CricketHotBattleLogDaoImpl extends TempDataObjectDao<DbCricketHotBattleLog> {


    public static CricketHotBattleLogDaoImpl newInstance() {
        CricketHotBattleLogDaoImpl dao = new CricketHotBattleLogDaoImpl();
        return dao;
    }

    @Override
    protected boolean updateData(DbCricketHotBattleLog data) {
        return true;
    }

    public static String insertSql = "insert into `t_c_cricket_hot_battle_log`(battleLogId,userId,userId1,battleType,score,userBaseInfoList,cricketBattleLog,createTime,updateTime) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DbCricketHotBattleLog data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<>();
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getBattleLogId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUserId1()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getBattleType()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, data.getScore()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, Helper.toJSONString(data.getUserBaseInfoList())));
        String encodeToString = Base64.getEncoder().encodeToString(data.getCricketBattleLog().toByteArray());
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, encodeToString));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        if (getDb().executeNoneQuery(insertSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }


    @Override
    protected DbCricketHotBattleLog parseData(ResultSet rs) throws SQLException {
        DbCricketHotBattleLog data = DbCricketHotBattleLog.getInstance();
        data.setBattleLogId(rs.getLong("battleLogId"));
        data.setUserId(rs.getLong("userId"));
        data.setUserId1(rs.getLong("userId1"));
        data.setBattleType(rs.getInt("battleType"));
        data.setScore(rs.getString("score"));
        data.setUserBaseInfoList(Helper.toObject(rs.getString("userBaseInfoList"), new TypeReference<List<CricketUserBaseInfo>>() {
        }));
        String matchRoundGroupString = rs.getString("cricketBattleLog");
        byte[] decode = Base64.getDecoder().decode(matchRoundGroupString);
        try {
            CricketProto.CricketBattleLog matchRoundGroup = CricketProto.CricketBattleLog.parseFrom(decode);
            data.setCricketBattleLog(matchRoundGroup);
        } catch (Exception e) {
            e.printStackTrace();
        }
        data.setCreateTime(rs.getLong("createTime"));
        data.setUpdateTime(rs.getLong("updateTime"));

        return data;
    }


    public Map<Long, DbCricketHotBattleLog> getDataMap(int limit) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, DbCricketHotBattleLog> dataMap = new ConcurrentHashMap<>();
        String selectSql = "select * from t_c_cricket_hot_battle_log order by battleLogId desc limit ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setInt(1, limit);
            rs = call.executeQuery();
            while (rs.next()) {
                DbCricketHotBattleLog data = parse(rs);
                dataMap.put(data.getBattleLogId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public boolean saveData(DbCricketHotBattleLog data) {
        if (data.isUpdateOption()) {
            if (data.isNeedInsert()) {
                if (addData(data)) {
                    data.setNeedInsert(false);
                }
            }
        }
        return true;
    }

}
